package com.example.service.impl;

import com.example.mapper.CourseMapper;
import com.example.model.Admin;
import com.example.model.Course;
import com.example.service.CourseService;
import com.example.util.DataMap;
import com.example.util.ErrorStatusEnum;
import com.example.util.MD5Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: tao95
 * Date: 2017/5/9
 * Time: 19:50
 * To change this template use File | Settings | File Templates.
 * Description:
 */
@Service
public class CourseServiceImpl implements CourseService{
    @Autowired
    private CourseMapper courseMapper;

    private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
    @Override
    public DataMap selectCourseList() {
        DataMap dataMap = new DataMap();
        List<Course> courseList = courseMapper.selectCourseList();
        dataMap.addAttribute("courseList",courseList);
        return dataMap;
    }
    @Override
    @Transactional
    public DataMap insertCourse(Course course) {
        DataMap dataMap = new DataMap();
        if(course.getId() == null){
            try {
                courseMapper.insertCourse(course);
            }catch(Exception e){
                logger.error(e.getMessage());
                dataMap.put("code", ErrorStatusEnum.ADD_COURSEFAIL.getErrorCode());
                return dataMap;
            }
        }else{
            try{
                courseMapper.editCourse(course);
            }catch (Exception e){
                logger.error(e.getMessage());
                dataMap.put("code", ErrorStatusEnum.EDIT_COURSEFAIL.getErrorCode());
                return dataMap;
            }
        }
        dataMap.put("code", ErrorStatusEnum.SUCCESS.getErrorCode());
        return dataMap;
    }
    @Override
    @Transactional
    public DataMap delCourse(Integer id) {
        DataMap dataMap = new DataMap();
        try{
            courseMapper.delCourse(id);
        }catch (Exception e){
            logger.error(e.getMessage());
            dataMap.put("code", ErrorStatusEnum.DEL_COURSEFAIL.getErrorCode());
            return dataMap;
        }
        dataMap.put("code", ErrorStatusEnum.SUCCESS.getErrorCode());
        return dataMap;
    }
}